Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to apply `mavg` to my own data, given external data is loaded?

Hi everyone,

I tried to calculate and plot moving average of close price of external data loaded into quantopian. I managed to plot close price. After tried a few times on mavg and SimpleMovingAverage, however, I still cannot get it right. I have no idea where went wrong. Could you help?

Thanks a lot!

the format of my data:

Date,symbol,open,high,low,close,volume
2010/04/16,IF,3450,3488,3413.2,3415.6,48988
2010/04/19,IF,3396,3396.2,3168.4,3197.8,109728

Here is my code:

from quantopian.pipeline.factors import SimpleMovingAverage  
def initialize(context):  


    fetch_csv('https://myfile/data.csv',  
               date_column = 'Date',  
               date_format = '%Y/%m/%d',  
               pre_func = preview,  
               symbol='IF')  
    context.stock = symbol('IF')  
def preview(df):  
    log.info(df.close.head())  
    return df  

def handle_data(context, data):

    current_price = data['IF']['close']  
    sma_10 = SimpleMovingAverage(inputs=data['IF']['close'], window_length=10)  
    sma_30 = SimpleMovingAverage(inputs=data['IF']['close'], window_length=30)  
    sma_20 = data['IF']['close'].mavg(20)  
    sma_50 = data['IF']['close'].mavg(50)  
    record(MA1 = sma_10, MA2 = sma_50, Price = current_price)  
6 responses

Hi Kenny,

The built-in SimpleMovingAverage factor is a component of the Pipeline API. Pipeline works with a list of datasets native to Quantopian. That list can be found here. If you want to calculate the SMA of your own imported data, you will have to create your own custom function to do so.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

@Jamie McCorriston Thanks for your reply. Can I use Ta-lib inside quantopian to write my own custom function to use my own imported data?

Yes, there's an example here from the help docs on using Ta-lib. That being said, it's important to note that if you trade IF stock, it will trade at the prices that we have in our database, so you will only be able to use your imported prices as signal.

Hi Jamie,

Thanks again for your help.

I just realized that there is another data called "IF" in your database and IF I used is the name I call my own data and I should not use context.stock = symbol('IF'), as doing it I actually imported IF data from quantopian database.

Here is what I intended to do:
1. I imported a Future prices, let me rename it to be 'FPCN' ( done)
2. I want to backtest of trading 'FPCN' (failed so far); in other word, can I somehow make context.stock = symbol('FPCN') possible?
3. I want to use history() upon close_price, open_price, high and low data of FPCN (failed so far)
4. I want to make set_benchmark(data['FPCN']) or `set_benchmark(symbol('FPCN')) working (failed so far)

The problem seems to be:

  1. (worst) Quantopian does not accept imported data to be traded object for backtest, in order words, quantopian only accept imported data as signals to help trade only symobls inside quantopian's database?
  2. history() only work with data from quantopian database, refuse to work on imported data? This means I have to calculate signals without a great tool of Quantopian?

I sincerely hope I am wrong about these two possible problems above.

are there ways to achieve what I want to do on list 2,3,4?

Is there a way to fake FPCN as a symbol of quantopian?
Is it possible to fake FPCN as a symbol to backtest inside Research Environment?

Thanks a lot?

Hi Kenny,

You can only trade the securities available in the Quantopian database (US equities). You can import external data via Fetcher and use this as a signal input to your algo or to create a list of stocks to trade, but they must be securities that are supported in our system.

That said, if you'd like to backtest other tradable assets, you can use Zipline. This is our open-sourced backtester and it's used to power the online IDE. You can use the project to work locally and if you have questions, there's a separate Google group for discussions.

We're working to add other classes to Quantopian, and Futures are next coming to Quantopian.

Cheers,
Alisa

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks a lot Alisa, now I am clear about this problem now.

also I am looking forward to trying out Futures on Quantopian, and good luck for the development.

Thanks again.